package utilidades.number;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import javafx.beans.InvalidationListener;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Control;
import javafx.scene.control.TextField;
import javafx.util.converter.NumberStringConverter;

/**
 * Fertonani oscar
 */
public class FloatTextField extends TextField {

   
    private SimpleFloatProperty number = new SimpleFloatProperty();
   
    //Si quiero q no tenga puntos ni nada....
   // NumberFormat nf =  new DecimalFormat("###");
    
    //Si quiero q este separado por tres puntos
    NumberFormat nf =  new DecimalFormat();
    
   
    
   
   
   
   
    

    public final Float getNumber() {
        return number.get();
    }

    public final void setNumber(Float value) {
        number.set(value);
    }

    public SimpleFloatProperty numberProperty() {
        return number;
    }

    public FloatTextField() {

        
       super();
       
       /*
     // simbolo.setDecimalSeparator('-');
      simbolo.setGroupingSeparator('-');
      final  DecimalFormat nf =  new DecimalFormat("###,###",simbolo);
        */
       
       
       textProperty().bindBidirectional(number, nf);
       this.setText("");
       
       focusedProperty().addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (!newValue.booleanValue()) {
                    if(number.get()==0){
                          setText("");
                    }else{
                    setText(nf.format(number.get()));
                    }
                }
            }
        });
      
       
        
       /* final ChangeListener escuchador=new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> textProperty, String oldValue, String newValue) {
               // System.out.println("cambio el property entra " + oldValue + " " + newValue);
                if( !oldValue.equals(newValue)){
                    
                if (newValue.trim().isEmpty() ) {
                 //   System.out.println("nuevo valor no es numero " + newValue);
                    setNumber(0);
                    return;
                }

                
                if (!newValue.matches("\\d+") ) {
                   // System.out.println("nuevo valor no es numero " + newValue);
                    ((StringProperty) textProperty).setValue(oldValue);

                } else {
                    try {
                        setNumber(Integer.parseInt(newValue));
                        
                    } catch (NumberFormatException ex) {
                     //   System.out.println("error "+ex);
                        ((StringProperty) textProperty).setValue(oldValue);
                      
                    }
                }
            }
            }
        };
        
  
        setNumber(0);
        
      
      
        this.number.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> textProperty, Number oldValue, Number newValue) {
                if(newValue!=null){
                   // if(getText().equals())
                    if(!getText().equals(String.valueOf(number.get()))){
                    textProperty().removeListener(escuchador);
                    setText(String.valueOf(number.get()));
                    textProperty().addListener(escuchador);
                    }
                }
            
            }});
  
        this.textProperty().addListener(escuchador);
        
         /* this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
        @Override  public void handle(KeyEvent inputevent) {
            System.out.println("1: apreto "+inputevent);
              if (!inputevent.getCharacter().matches("\\d")) {              
                           inputevent.consume();
        }
            }
        });*/
    }
    
    
    


   

    

     /* @Override public void replaceText(int start, int end, String text) {
        // If the replaced text would end up being invalid, then simply
        // ignore this call!
         
        if (!text.matches("[a-z]|[A-Z]")) {
            super.replaceText(start, end, text);  
        }
       
    }
 
    @Override public void replaceSelection(String text) {
           
        if (!text.matches("[a-z]|[A-Z]")) {
            super.replaceSelection(text);
             this.number.set(Integer.parseInt(text));
        }
    }*/
    
    
    
    
    
}
